package com.jh.fcsm.controller.sys.rest;

import com.jh.fcsm.beans.sys.SysFileInfo;
import com.jh.fcsm.common.BaseController;
import com.jh.fcsm.config.FileServiceFactory;
import com.jh.fcsm.service.sys.FileService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 可匿名访问的文件操作接口
 */
@RestController
@RequestMapping("/sys/file/rest")
@Api(value = "文件上传rest服务", tags = "文件上传rest服务")
public class RestFileController extends BaseController {

    @Value("${file.useFileType}")
    private String fileSource;

    @Autowired
    private FileServiceFactory fileServiceFactory;

    /**
     * 文件上传
     *
     * @param file 文件
     * @return 文件信息
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public SysFileInfo upload(MultipartFile file) throws Exception {
        FileService fileService = fileServiceFactory.getFileService(fileSource);
        SysFileInfo sysFileInfo = new SysFileInfo();
        return fileService.upload(file, sysFileInfo);
    }
}
